﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base
{
    public partial class Base_QrCodeTypeBusiness : BaseBusiness<Base_QrCodeType>, IBase_QrCodeTypeBusiness, ITransientDependency
    {
        public Base_QrCodeTypeBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<Base_QrCodeType>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_QrCodeType>();
            var search = input.Search;

            //筛选
            if (!search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_QrCodeType, bool>(
                    ParsingConfig.Default, false, $@"Describe.Contains(@0) or Key.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Base_QrCodeType> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataAddLog(UserLogType.二维码字典, "Describe", "二维码字典")]
        [DataRepeatValidate(new string[] { "Key" }, new string[] { "类型编号" })]
        public async Task AddDataAsync(Base_QrCodeType data)
        {
            await InsertAsync(data);
        }

        [DataAddLog(UserLogType.二维码字典, "Describe", "二维码字典")]
        [DataRepeatValidate(new string[] { "Key" }, new string[] { "类型编号" })]
        public async Task UpdateDataAsync(Base_QrCodeType data)
        {
            await UpdateAsync(data);
        }

        [DataAddLog(UserLogType.二维码字典, "Describe", "二维码字典")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        /// <summary>
        /// Base64解析
        /// </summary>
        /// <param name="QrCode"></param>
        /// <returns></returns>
        public string GetTheDataAsyncs(string QrCode)
        {
            //解析Base64
            string b64 = QrCode.Replace("\r\n", "");
            string result = Extention.Base64UrlDecode(b64);
            var bqt = JsonConvert.DeserializeObject(result);
            return bqt.ToString();
        }

        #endregion

        #region 私有成员

        #endregion
    }
}